package com.etc.service.impl;

import com.etc.dao.BooksMapper;
import com.etc.dao.BookstypeMapper;
import com.etc.entity.Books;
import com.etc.entity.Bookstype;
import com.etc.service.BooksService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * xst
 */

@Service
public class BooksServiceImpl implements BooksService {
    @Autowired
    BooksMapper dao;
    BookstypeMapper dao2;

    /**
     * 删除商品
     * @param bId 商品id
     * @return true 删除成功 false 删除失败
     */
    @Override
    public boolean deleteByPrimaryKey(Integer bId) {
        int i = dao.deleteByPrimaryKey(bId);
        return i>0;
    }

    /**
     * 新增商品
     * @param record 商品信息
     * @return true 增加成功 false 增加失败
     */
    @Override
    public boolean insert(Books record) {
        int i = dao.insert(record);
        return i>0;
    }

    /**
     * 模糊查询
     * @param bName 商品名称关键字
     * @return 查询到的商品信息
     */
    @Override
    public PageInfo<Books> selectByPrimaryKey(String bName, int pageNum, int size) {
        PageHelper.startPage(pageNum, size);
        List<Books> list = dao.selectByPrimaryKey("%" + bName + "%");
        PageInfo<Books> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 修改商品
     * @param record 修改的信息
     * @return true 修改成功 false 修改失败
     */
    @Override
    public PageInfo<Books> updateByPrimaryKey(Books record,int pageNum, int size) {
        int i = dao.updateByPrimaryKey(record);
        PageInfo<Books> pageInfo = selectByPrimaryKey(record.getbName(), pageNum, size);
        return pageInfo;
    }


    @Override
    public List<Books> getAllBooks() {
        return dao.selectAllBooks();
    }

    @Override
    public Books getById(Integer bId) {
        return dao.selectById(bId);
    }

    @Override
    public List<Books> getByType(String btName, int pageNum, int size) {
        PageHelper.startPage(pageNum, size);
        return dao.selectByType(btName);
    }

    @Override
    public List<Books> getByLike(String bName, String bAuthor,String btName,int pageNum, int size) {
        PageHelper.startPage(pageNum, size);
        System.out.println(btName);
        return dao.selectByLike("%"+bName+"%","%"+bAuthor+"%",btName);

    }

    @Override
    public List<Bookstype> getAllType() {
        return dao2.selectAllType();
    }
}
